"""
1。提取到主页面中的每一个电影的背后的那个url地址
    1。拿到“2023必看热片” 那一块的HTML代码
    2.从刚才拿到的HTML代码中提取到href的值
2。访问子页面，提取到电影的名称以及下载地址
    1.拿到子页面的源代码
    2.数据提取
"""
import requests
import re

url = "https://dy2018.com"
response = requests.get(url)
response.encoding = 'gb2312'
# print(response.text)

# 提取代码
obj1 = re.compile(r"2023必看热片.*?<ul>(?P<html>.*?)</ul>", re.S)
r1 = obj1.search(response.text)
html = r1.group('html')
# print(html)
# 提取html的herf的链接
obj2 = re.compile(r"<li><a href='(?P<href>.*?)' title", re.S)
r2 = obj2.finditer(html)

# 提取片名
obj3 = re.compile(r'<div id="Zoom">.*?◎片　　名(?P<movie>.*?)<br />'
                  r'.*?<td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="'
                  r'(?P<download>.*?)">', re.S)

for result in r2:
    # print(result.group("href"))
    child_url = url + result.group("href")
    # print(child_url)
    child_response = requests.get(child_url)
    child_response.encoding = "gbk"
    # print(child_response.text)
    r3 = obj3.search(child_response.text)
    movie = r3.group('movie')
    download = r3.group('download')
    print(movie, download)
